<?php
/**
* Prepare graph data
*
* @author       Fabrice Douteaud <admin@clearbudget.net>
* @package      actions
* @access       public
*/

/***********************************************************************

  Copyright (C) 2008  Fabrice Douteaud (admin@clearbudget.net)

    This file is part of ClearBudget.

    ClearBudget is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClearBudget is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClearBudget.  If not, see <http://www.gnu.org/licenses/>.


************************************************************************/
//prevent direct access
if(!defined('ENGINEON')) die('Direct access forbidden');

$report = class_report::getInstance();
$report->getData();
if($report->validData === false) {
  $error = 'error_NoTransactionData';
  $backAction = 'tableOverview';
  $nextAction = 'uploadQif';
  $context->snippet = 'errorDisplay';
  return;
  }
  
$dates = $report->dates;
$totalDebits = $report->totalDebits;
$debitsPctPerMonth = $report->debitPctPerMonth;
$creditsPctPerMonth = $report->creditPctPerMonth;
$debitPerCategoryPerMonth = $report->debitPerCategoryPerMonth;
$creditPerCategoryPerMonth = $report->creditPerCategoryPerMonth;
$debitTotalPerMonth = $report->debitTotalPerMonth;
$creditTotalPerMonth = $report->creditTotalPerMonth;
$debitTotalPerCategory = $report->debitTotalPerCategory;
$categories = $report->categories;
$categoryColor = $report->categoryColor;

// Overall credit vs debit graph
$creditPct = $report->formatNumber($report->totalCredits/($report->totalDebits+$report->totalCredits) * 100);
$debitPct = $report->formatNumber($report->totalDebits/($report->totalDebits+$report->totalCredits) * 100);
$chart = new GoogleChart('p3', $keys->graphTitle_overallDebitsVsCredits, 250, 100);
$chart->addLegend($debitPct.'%');
$chart->addValue($debitPct);
$chart->addColor('FF2725');
$chart->addLegend($creditPct.'%');
$chart->addValue($creditPct);
$chart->addColor('97EE14');
$url1 = $chart->getURL();

// monthly spending VS credits
$chart = new GoogleChart('bvs', $keys->graphTitle_monthlyDebitsVsCredits, 450, 150);
$chart->addColor('97EE14');
$chart->addColor('FF2725');
$chart->addLegend($keys->text_Credit);
$chart->addLegend($keys->text_Debit);
foreach($dates as $date) {
  $month = explode('-', $date);
  $chart->addxAxis($month[0]);
  }
foreach($creditTotalPerMonth as $month=>$credit) {
  @$chart->addValue($credit/($debitTotalPerMonth[$month]+$credit) * 100);
  }
$chart->addValueSerie();
foreach($debitTotalPerMonth as $month=>$debit) {
  @$chart->addValue($debit/($creditTotalPerMonth[$month]+$debit) * 100);
  }
$chart->addyAxis('0|20|40|60|80|100');
$url2 = $chart->getURL();
 
// top 5 categories overall spending
$chart = new GoogleChart('p3', $keys->graphTitle_top5DebitsOverall, 550, 200);
if(count($debitTotalPerCategory) > 0) {
  // sort by value
  asort($debitTotalPerCategory, SORT_NUMERIC);
  $debitTotalPerCategory = array_reverse($debitTotalPerCategory, true);
  // keep only the top 5 items
  $top5DebitsPerCategory = array_slice($debitTotalPerCategory, 0, 5, true);
  // keep all other items
  $otherDebits = array_slice($debitTotalPerCategory, 5, null, true);
  // build the colors array
  foreach($top5DebitsPerCategory as $key=>$val) {
    $val = $report->formatNumber($val / $totalDebits * 100);
    $chart->addLegend($report->getCategoryName($key).' ('.$val.'%)');
    $chart->addValue($val);
    $chart->addColor($categoryColor[$key]);
    }
  // compute the overall value of all 'others'
  $tmpVal = 0;
  foreach($otherDebits as $key=>$val) {
    $tmpVal += $val;
    }
  $tmpVal = $report->formatNumber($tmpVal / $totalDebits * 100);
  $chart->addLegend('Others ('.$tmpVal.'%)');
  $chart->addValue($tmpVal);
  $chart->addColor('444444');
  $url3 = $chart->getURL();
  }

// top 5 categories monthly spending
$chart = new GoogleChart('bvs', $keys->graphTitle_top5DebitsMonthly, 450, 150);
// reverse the array as to get the smallest first
$top5DebitsPerCategory = array_reverse($top5DebitsPerCategory, true);
// go through each item
foreach($top5DebitsPerCategory as $category=>$val) {
  // category color
  $chart->addColor($categoryColor[$category]);
  // graph legend (cartegory name)
  $chart->addLegend($report->getCategoryName($category));
  // go through each month
  foreach($dates as $date) {
    // check if there is spending for that month
    if(isset($debitPerCategoryPerMonth[$category][$date])) {
      $chart->addValue($report->formatNumber($debitPerCategoryPerMonth[$category][$date] / $debitTotalPerMonth[$date] * 100));
      }
    else {
      $chart->addValue('0.0');
      }
    }
  $chart->addValueSerie();
  }
foreach($dates as $date) {
  $month = explode('-', $date);
  $chart->addxAxis($month[0]);
  }
$chart->addyAxis('0|20|40|60|80|100');
$url4 = $chart->getURL();

?>